package com.sen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.dao.SellDao;
import com.sen.dao.SellheadDao;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.MenuEntity;
import com.sen.entity.SellEntity;
import com.sen.entity.SellheadEntity;
import com.sen.service.SellheadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("sellheadService")
public class SellheadServiceImpl extends ServiceImpl<SellheadDao, SellheadEntity> implements SellheadService {

    @Autowired
    SellheadDao sellheadDao;
    @Autowired
    SellDao sellDao;
    @Override
    public IPage<SellheadEntity> queryAll(Page<SellheadEntity> page, Map<String, String> sellheadMap) {
        QueryWrapper<SellheadEntity> wrapper = new QueryWrapper<>();
        if (sellheadMap.get("sid")!=null){
            wrapper.like("sid", sellheadMap.get("sid"));
        }
        if (sellheadMap.get("createdate[0]")!=null){
            wrapper.between("DATE_FORMAT(saledate,'%Y-%m-%d')", sellheadMap.get("createdate[0]"), sellheadMap.get("createdate[1]"));
        }
        //查询所有销售信息及审核人，出售人
        IPage<SellheadEntity> sellheadEntityPage = sellheadDao.queryAll(page, wrapper);
        //遍历所有订单表头查出所有对应信息
        for (SellheadEntity sellhead : sellheadEntityPage.getRecords()) {
            List<SellEntity> sells = sellDao.selectAllSellinfo(sellhead.getSid());
            sellhead.setChildren(sells);
        }
        return sellheadEntityPage;
    }
    @Override
    public Map<String, Object> getDataSta(String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<SellheadEntity> wrapper = new QueryWrapper<>();
        wrapper.between("saleDate",begin,end);
        wrapper.select("saleDate","sum(money) money");
        wrapper.groupBy("DATE_FORMAT(saleDate,'%Y-%m-%d')");
        wrapper.orderByAsc("saleDate");
        List<SellheadEntity> sellheadEntities = baseMapper.selectList(wrapper);


        //因为返回有两部分数据：日期和日期对应金额
        //前端要数组json结构 对应java代码时list集合
        //创建两个list集合，一个是日期list 一个是价格list
        List<String> saleDate = new ArrayList<>();
        List<BigDecimal> priceDataList = new ArrayList<>();
        for (SellheadEntity sellheadEntity : sellheadEntities) {
            Date date = new Date(sellheadEntity.getSaledate().toString());
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            saleDate.add(sdf.format(date));
            priceDataList.add(sellheadEntity.getMoney());
        }

        //把封装之后的两个list集合放到map中返回
        Map<String,Object> map = new HashMap<>();
        map.put("saleDate",saleDate);
        map.put("priceDataList",priceDataList);

        return map;
    }
}
